package com.aios.service.impl;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.aios.dao.SwitchPanelMapper;
import com.aios.dao.SystemUserHasSwitchPanelMapper;
import com.aios.dao.SystemUserMapper;
import com.aios.dao.UsageDataMapper;
import com.aios.entity.Switch;
import com.aios.entity.SwitchPanel;
import com.aios.entity.UsageData;
import com.aios.exception.AiosStatusUpdateException;
import com.aios.service.SystemUserService;
import com.aios.service.UsageDataService;
import com.aios.util.AiosAccessUtil;
import com.aios.util.GuidGenerator;
import com.aios.util.HttpClientExecuter;
import com.aios.util.JsonConvertor;
import com.aios.util.HttpClientAdaptor.HttpClientResponse;

@Service("userDataService")
public class UsageDataServiceImpl extends AbstractService implements UsageDataService {

	
	private HashMap<String, List<Switch>> PreviousStatus = new HashMap<String, List<Switch>>();
	private List<Switch> previousSwitchList;
	private List<Switch> currentSwitchList;
	private List<SwitchPanel> currentSwitchPannelList;
	private Float currentAmperage;
	private Float currentWattage;
	
	@Autowired
	private SwitchServiceImpl switchServiceImpl;
	@Autowired
	private SystemUserHasSwitchPanelMapper systemUserHasSwitchPanelMapper;
	@Autowired
	private SwitchPanelMapper switchPanelMapper;
	@Autowired
	private UsageDataMapper usageDataMapper;
	
	private UsageData record;
	
	Calendar cal;
	
	public Float currentAmperage(String switchId) {

		/*SwitchPanel switchPanel = switchPanelMapper.findById(panelId);

		if (switchPanel == null) {
			return null;
		}

		HttpClientResponse response = HttpClientExecuter
				.getAiosControllerResponse(switchPanel.getProtocol(), switchPanel.getUrl(),
						switchPanel.getHttpPort(), switchPanel.getUserName(),
						switchPanel.getUserPassword(),
						AiosAccessUtil.STATUS_URI, true);

		if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {

			try {
				List<Switch> switchList = switchMapper.findAllBySwitchPanelId(panelId);

				Map<String, String> map = JsonConvertor.convertAiosStateResponse(response.getResponseText());

				if (switchList != null && map != null) {
					for (Switch s : switchList) {
						s.setCurrentState(map.get(s.getControllerReceiver()));
					}
				} else {
					logger.error("data not recieved from DB or AIOS");
				}

				return switchList;

			} catch (AiosStatusUpdateException e) {
				logger.error(e);
			}

		}

		return null;
		*/
		return (float) 5.00;

	}
	
	public Float currentWattage(String switchId) {

		/*SwitchPanel switchPanel = switchPanelMapper.findById(panelId);

		if (switchPanel == null) {
			return null;
		}

		HttpClientResponse response = HttpClientExecuter
				.getAiosControllerResponse(switchPanel.getProtocol(), switchPanel.getUrl(),
						switchPanel.getHttpPort(), switchPanel.getUserName(),
						switchPanel.getUserPassword(),
						AiosAccessUtil.STATUS_URI, true);

		if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {

			try {
				List<Switch> switchList = switchMapper.findAllBySwitchPanelId(panelId);

				Map<String, String> map = JsonConvertor.convertAiosStateResponse(response.getResponseText());

				if (switchList != null && map != null) {
					for (Switch s : switchList) {
						s.setCurrentState(map.get(s.getControllerReceiver()));
					}
				} else {
					logger.error("data not recieved from DB or AIOS");
				}

				return switchList;

			} catch (AiosStatusUpdateException e) {
				logger.error(e);
			}

		}

		return null;
		*/
		return (float) 1500.00;

	}
	
	
	//@Scheduled(cron = "0 06,11,16,21,26,31,36,41,46,51,56 * * * ?")
		//@Scheduled(cron = "0/10 * * * * ?")
		public void switchUsageUpdate() {/*
			
			previousSwitchList = new ArrayList<Switch>();
			currentSwitchList = new ArrayList<Switch>();
			currentSwitchPannelList = new ArrayList<SwitchPanel>();
			currentSwitchPannelList = switchPanelMapper.findall();
			
			for(SwitchPanel sp : currentSwitchPannelList){
				
				//System.out.println("Pannel Id: "+sp.getId());
				currentSwitchList = switchServiceImpl.switchStatus(sp.getId());
				
			if(PreviousStatus.get(sp.getId()) != null){
					previousSwitchList = PreviousStatus.get(sp.getId());
					
					if(currentSwitchList!=null){
						for(int i=0;i<currentSwitchList.size();i++){
							//System.out.println("Pre: "+previousSwitchList.get(i).getCurrentState()+" Cur: "+currentSwitchList.get(i).getCurrentState());
							
							if(!previousSwitchList.get(i).getCurrentState().equals(currentSwitchList.get(i).getCurrentState())){
								
								currentAmperage = currentAmperage(currentSwitchList.get(i).getId());
								currentWattage = currentWattage(currentSwitchList.get(i).getId());
								
								cal=Calendar.getInstance();
								record = new UsageData(systemUserHasSwitchPanelMapper.findByPannelId(sp.getId()),sp.getId(),currentSwitchList.get(i).getId(),currentSwitchList.get(i).getCurrentState(),cal.getTime());
								record.setUsageId(GuidGenerator.usageDataKey());
								record.setCurrentAmperage(currentAmperage);
								record.setCurrentWatt(currentWattage);
								saveAddSettings(record,"Admin");
								usageDataMapper.insert(record);
							}
						}
					}else{
						System.out.println("Network Problem");
					}
					
					
					
				}
				PreviousStatus.put(sp.getId(), currentSwitchList);
			}*/
		}


	@Override
	public List<UsageData> findAll() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<UsageData> findAllNotDisabled() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<UsageData> findAllDisabled() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public UsageData findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public int save(UsageData entity) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int update(UsageData entity) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int delete(UsageData id) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int save(UsageData entity, String userId) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int update(UsageData entity, String userId) {
		// TODO Auto-generated method stub
		return 0;
	}
}
